#try the model f674>60 ->2, all others zero
df_test = readRDS("test.rds")

ord = order(df_test$f276, df_test$f521)
df_test = df_test[ord,]
df_test$f276[is.na(df_test$f276)]=0
next_f276 = rep(NA, nrow(df_test))
for(i in 1:nrow(df_test)-1) next_f276[i] = df_test$f276[i+1]
next_f276[nrow(df_test)] = 0
flag = (df_test$f276 != next_f276)
resp = ifelse(flag & (df_test$f674>60), 1, 0)
submission = data.frame(id = df_test$id, loss=resp)
submission = submission[order(submission$id),]
write.csv(submission, "submission.csv", row.names=F)



df_subset = df_train[flag,]


df_train = readRDS("train.rds")
length(unique(paste(df_train$f276, df_train$f521,sep="-")))
df_train$f276[is.na(df_train$f276)] = 0

ord = order(df_train$f276, df_train$f521)	#Order by "loan id" and payment sequence
df_train=df_train[ord,]
next_f276 = rep(NA, nrow(df_train))
next_f276[nrow(df_train)] = 0
for(i in 1:length(id)-1) next_f276[i] = df_train$f276[i+1]
df_subset = df_train[next_f276 != df_train$f276,]

transaction_cnt=tapply(rep(1, nrow(df_train)), df_train$f276, sum)
transaction_id = tapply(df_train$f276, df_train$f276, max)
df_trans = data.frame(f276=transaction_id, transaction_cnt)

x=merge(df_subset[,c("id","f276","f521","loss")], df_trans, by="f276")
tapply(x$loss>0, x$transaction_cnt, mean)



df_loan=data.frame(transaction_cnt=pmin(transaction_cnt,50), loss_amt, loss_ind = as.integer(loss_amt>0))
tapply(df_loan$loss_ind, df_loan$transaction_cnt, mean)

df_loan = data.frame(f276=as.numeric(row.names(df_loan)), df_loan)
model = tapply(df_loan$loss_amt, df_loan$transaction_cnt, median)
df_model = data.frame(transaction_cnt=as.numeric(names(model)), model)
df_pred = merge(df_loan, df_model, by="transaction_cnt")

df_pred2 = data.frame(id=df_train$id, f276=df_train$f276, f521=df_train$f521, loss=df_train$loss)
df_pred3 = merge(df_pred2, df_pred[,c(2,5)], by="f276")
ord = order(df_pred3$f276, df_pred3$f521)
df_pred3 = df_pred3[ord,]





